package com.woniu.supermarket.web;

import com.woniu.supermarket.entity.Permission;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IPermissionService;
import com.woniu.supermarket.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(value = "菜单的操作")
@RequestMapping("menu")
public class MenuController {

    @Autowired
    IPermissionService permissionService;

    @GetMapping("/listMenu")
    @ApiOperation(value = "查询菜单列表")
    public ResultResponse<List<MenuVo>> listMenu(){
        ResultResponse<List<MenuVo>> result = null;
        try {
            List<MenuVo> list  = permissionService.queryMenu();
            result =  new ResultResponse<>(200,"查询菜单成功",list);
        }catch (RuntimeException e){
            e.printStackTrace();
            result =  new ResultResponse<List<MenuVo>>(200,"查询菜单失败");
        }
        return result;
    }

    @GetMapping("/parentMenuList")
    @ApiOperation(value = "查询父菜单的方法")
    public ResultResponse<List<Permission>> parentMenuList(){
        ResultResponse<List<Permission>> result = null;

        try {
            List<Permission> list = permissionService.queryParentMenu();
            if(list != null){
                result = new ResultResponse<>(200,"查询父菜单成功",list);

            }else{
                result = new ResultResponse<>(201,"查询父菜单失败");

            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }


    @GetMapping("/listSomeoneMenu/{userName}")
    @ApiOperation(value = "查询某个用户菜单列表")
    public ResultResponse<List<MenuVo>> listSomeoneMenu(@ApiParam(value = "用户名称") @PathVariable String userName){
        ResultResponse<List<MenuVo>> result = null;
        try {
            List<MenuVo> list  = permissionService.queryMenuOfSomeone(userName);
            if(list!=null){
                result =  new ResultResponse<>(200,"查询用户菜单成功",list);
            }else{
                result =  new ResultResponse<>(201,"查询用户菜单失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result =  new ResultResponse<List<MenuVo>>(555,"系统错误");
        }
        return result;
    }


    @GetMapping("/listAllPermissions")
    @ApiOperation(value = "查询所有权限")
    public ResultResponse<List<MenuVo>> listAllPermissions(){
        ResultResponse<List<MenuVo>> result = null;
        try {
            List<MenuVo> list  = permissionService.queryAllPermissions();
            if(list != null){
                result =  new ResultResponse<>(200,"查询所有权限成功",list);
            }else{
                result =  new ResultResponse<>(201,"查询所有权限失败");

            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result =  new ResultResponse<List<MenuVo>>(555,"系统错误");
        }
        return result;
    }



}
